// index.js
// const app = getApp()
const { envList } = require('../../envList.js');
Page({
  data: {
      equipment_list: '',
      field_list: '',
      showView: true,
      btn_select: true,
      index_show: false
  },
  onLoad(options) {
    this.getEquipment();
    this.getField();
  },
  onShow() {
    // 小程序初始化时检查该用户是否进行过注册
    wx.cloud.callFunction({
        name: 'quickstartFunctions',
        config: {
        env: 'cloud1-1gtht6ez27113455'
        },
        data: {
            type: 'getOpenId'
        }
    }).then((resp) => {
        let openid = getApp().globalData.openid;
        if(openid==undefined){
            getApp().globalData.openid = resp.result.openid;
        }
    })

  },
  onClickPowerInfo(e) {
    const index = e.currentTarget.dataset.index;
    const powerList = this.data.powerList;
    powerList[index].showItem = !powerList[index].showItem;
    if (powerList[index].title === '数据库' && !this.data.haveCreateCollection) {
      this.onClickDatabase(powerList);
    } else {
      this.setData({
        powerList
      });
    }
  },

  onChangeShowEnvChoose() {
    wx.showActionSheet({
      itemList: this.data.envList.map(i => i.alias),
      success: (res) => {
        this.onChangeSelectedEnv(res.tapIndex);
      },
    });
  },

  onChangeSelectedEnv(index) {
    if (this.data.selectedEnv.envId === this.data.envList[index].envId) {
      return;
    }
    const powerList = this.data.powerList;
    powerList.forEach(i => {
      i.showItem = false;
    });
    this.setData({
      selectedEnv: this.data.envList[index],
      powerList,
      haveCreateCollection: false
    });
  },

  jumpPage(e) {
      let id = e.currentTarget.dataset.page[0];
      let state = e.currentTarget.dataset.page[1];
      let type = e.currentTarget.dataset.page[2];
      if(type=='eq'){
          if(state != 0){
            wx.showToast({
              title: '该设备无法预定',
              icon: 'error'
            })
          }else{
            getApp().globalData.ID = e.currentTarget.dataset.page[0]
            getApp().globalData.type = e.currentTarget.dataset.page[2]
            wx.navigateTo({
                url: `/pages/apply/index`,
              });
          }
      }else if(type=='field'){
          if(state >= 10){
            wx.showToast({
                title: '该场地预定已满',
                icon: 'error'
              })
          }else{
            getApp().globalData.ID = e.currentTarget.dataset.page[0]
            getApp().globalData.type = e.currentTarget.dataset.page[2]
            wx.navigateTo({
              url: '/pages/apply/index',
            })
          }
      }
  },

  onClickDatabase(powerList) {
    wx.showLoading({
      title: '',
    });
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      config: {
        env: this.data.selectedEnv.envId
      },
      data: {
        type: 'createCollection'
      }
    }).then((resp) => {
      if (resp.result.success) {
        this.setData({
          haveCreateCollection: true
        });
      }
      this.setData({
        powerList
      });
      wx.hideLoading();
    }).catch((e) => {
      this.setData({
        showUploadTip: true
      });
      wx.hideLoading();
    });
  },
  start() {
    this.setData({
      index_show: true
    })
  },
  getEquipment() {
      const db = wx.cloud.database();
      db.collection('equipments').orderBy('state', 'asc').get().then(res => {
        this.setData({
            equipment_list: res.data
        });
    })
  },
  getField() {
    const db = wx.cloud.database();
    db.collection('fields').orderBy('state', 'asc').get().then(res => {
        this.setData({
            field_list: res.data
        })
    })
  },
  switchToField() {
      this.setData({
          'showView': true,
          'btn_select': true
      })
  },
  switchToEq() {
      this.setData({
          'showView': false,
          'btn_select': false
      })
  }
});
